/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author adrian
 * @author olga
 */
public class UsersList extends ArrayList<User> {

    private User localUser;

    public UsersList() {
    }

    public UsersList(User localUser) throws UnknownHostException {
        String transfert = localUser.getAddr().getHostAddress();

        this.localUser = localUser;
        this.localUser.setAddr(InetAddress.getByName(transfert));
        this.add(localUser);
    }

    /**
     * This method is aimed in adding a new user the user list
     *
     * @param user is the user that is going to be added to the list
     */
    public void addUser(User user) {
        this.add(user);
    }

    /**
     * This method is aimed in removing a user from the user list
     *
     * @param user is the user that is going to be removed to the list.
     */
    public void removeUser(User user) {
        this.remove(user);
    }

    /**
     * Get local user.
     *
     * @return localUser Local user.
     */
    public User getLocalUser() {
        return localUser;
    }

    /**
     * Set localu user.
     *
     * @param localUser New local user
     */
    public void setLocalUser(User localUser) {
        this.localUser = localUser;
    }

    /**
     * Get a specific user from the list.
     *
     * @param user User to return.
     * @return user The real user from the list.
     */
    public User get(User user) {
        return this.get(this.indexOf(user));
    }

    /**
     * Get local user name.
     *
     * @return name The name of the local user.
     */
    public String getLocalUsername() {
        return localUser.getLogin();
    }

    public boolean contains(User user) {
        boolean bool = false;
        int size = this.size();
        for (int i = 0; i < size; i++) {
            if (user.equals(this.get(i))) {
                bool = true;
            }
        }
        return bool;
    }

    /**
     *
     * @param user
     * @return the user's index in the list
     */
    public int indexOf(User user) {
        int dummy = -1;
        int size = this.size();
        for (int i = 0; i < size; i++) {

            if (user.equals(this.get(i))) {
                dummy = i;
            } else {

            }
        }
        return dummy;
    }

    /**
     *
     * @param login the user's login
     * @return the user matching with the login
     */
    public User getUserByLogin(String login) {
        User dummy = localUser;
        int size = this.size();
        for (int i = 0; i < size; i++) {

            if (login.equals(this.get(i).getLogin())) {
                dummy = this.get(i);
            } else {

            }
        }
        return dummy;
    }

}
